package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.logic.rank.UnionRankMsgModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PawnshopProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.CommonActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Map;
import java.util.Objects;

@Cmd(code = Protocol.S_PEAK_UNION_MAX_ABILITY_RANK_LIST, desc = "请求巅峰报名期数据,做成通用的报名期历史最高赚钱排行")
public class PeakActivityUnionMaxAbilityRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (null == activityInfo) {
            return;
        }

        if (activityInfo.getType() != eActivityType.PeakSeacraftActivity.getValue() &&
                activityInfo.getType() != eActivityType.TonBingActivity.getValue() &&
                activityInfo.getType() != eActivityType.FiveHero.getValue()) {
            return;
        }

        RankProto.GetActivityUnionRankListRespMsg.Builder respMsg = RankProto.GetActivityUnionRankListRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setMyValue("0");
        respMsg.setMyRank(0);
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
        boolean isGroupType = UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType());
        UnionRankMsgModel model = CommonActivityRankMgr.getUnionRankMsgModelByGroup(activityId, groupId);
        if (null != model) {
            RankProto.UnionRankListMsg rankListMsg = model.getRankListMsg();
            String unionUid = player.getUserInfo().getUnionUid();
            if (isGroupType) {
                unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, player.getUserId());
            }
            boolean isInside = false;
            int rank = 0;
            for (RankProto.UnionRankTempMsg rankTempMsg : rankListMsg.getRankListList()) {
                rank++;
                if (Objects.equals(rankTempMsg.getUnionUid(), unionUid)) {
                    respMsg.setMyValue(rankTempMsg.getValue());
                    respMsg.setMyRank(rank);
                    isInside = true;
                    break;
                }
            }
            if (!isInside && !StringUtils.isNullOrEmpty(unionUid)) {//不在榜单里,累计游戏服缓存的赚钱
                Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
                BigInteger myValue = BigInteger.ZERO;
                for (CrossUnionMember member : memberMap.values()) {
                    myValue = myValue.add(member.getHistoryMaxAbility());
                }
                respMsg.setMyValue(myValue.toString());
            }

            respMsg.setRank(rankListMsg);
            player.sendPacket(Protocol.U_PEAK_UNION_MAX_ABILITY_RANK_LIST, respMsg);

            if (model.isNeedUpdate()) {
                if(PeakSeacraftMgr.hasActivity() && PeakSeacraftMgr.getActivityInfo().getActivityId() == activityId){
                    //判断是否有在报名期
                    if (!PeakSeacraftMgr.isInRegistration()) {
                        return;
                    }
                }
                if(TonBingMgr.hasActivity() && TonBingMgr.getActivityInfo().getActivityId() == activityId){
                    //判断是否有在报名期
                    if (!TonBingMgr.isInRegistration()) {
                        return;
                    }
                }
                if(FiveHeroMgr.hasActivity() && FiveHeroMgr.getActivityInfo().getActivityId() == activityId){
                    //判断是否有在报名期
                    if (!FiveHeroMgr.isInRegistration()) {
                        return;
                    }
                }
                model.addSynPlayer(player.getUserId());
                if (!model.isInReq()) {
                    //向商会跨服请求更新
                    CrossUnionProto.CrossUnionGetMaxAbilityUnionRankListReq.Builder crossReqMsg = CrossUnionProto.CrossUnionGetMaxAbilityUnionRankListReq.newBuilder();
                    crossReqMsg.setActivityId(activityId);
                    crossReqMsg.addAllServerIds(activityInfo.getServerIdList());
                    crossReqMsg.setRankNum(model.getRankNum());
                    crossReqMsg.setActivityEndTime(activityInfo.getEndShowTime());
                    crossReqMsg.setGroupId(model.getGroupId());
                    GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_UNION_GET_MAX_ABILITY_UNION_RANK_LIST, crossReqMsg));
                }
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
